<?php

function hgn_form_add_address_field(&$form){
  $form['address'] = array(
    '#type' => "fieldset",
    "#title"=> t("Address"),
    "#weight" => 8,
    "#tree" => TRUE,
    
    'text' => array(
      '#type'  => "textfield",
      '#title' => t("Address"),
      '#required'  => TRUE,
      '#weight'   => 1,
    ),
    'postal_code' => array(
      '#type'  => "textfield",
      '#title' => t("Postal Code"),
      '#required'  => TRUE,
      '#weight'   => 2,
      '#element_validate' => array("hgn_form_add_address_postal_code_field_validate"),
    ),
    // TODO: advanced feature: longitude and latitude
  );
}

function hgn_form_add_address_postal_code_field_validate($element,&$form_state,$form){
  if(empty($element['#value'])){
    form_error($element,"Postal Code field is required.");
  } else if(!preg_match("/^[0-9]{6}$/", trim($element['#value']))){
    form_error($element,"Please enter the correct format for the postal code, example: 123456");
  }
}

?>